package cn.migu.music.hangupsms.service.sftp;

import cn.migu.music.common.redis.IRedisClient;
import cn.migu.music.common.utils.LogUtils;
import cn.migu.music.hangupsms.common.config.HangUpSmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class DelBlackUserRedisCacheService {
    private static final String DEL_BLACK_USER_HEAD = "del-balck-user";

    @Autowired
    private IRedisClient        redisClient;
    @Autowired
    private HangUpSmsConfig     hangUpSmsConfig;

    /**
     * 缓存删除的黑名单用户
     * 
     * @param phoneNumber
     */
    public void delBlackUserCache(String phoneNumber) {
        try {
            String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            String key = getKey(dateStr);
            redisClient.hset(key, phoneNumber, "1");
            redisClient.expire(key, hangUpSmsConfig.getDelBlackUserCacheSeconds());
        } catch (Exception ex) {
            LogUtils.error(ex, log, "缓存删除的黑名单手机号异常");
        }
    }

    /**
     * 获取某天删除得黑名单用户
     * 
     * @param dateStr
     *            日期格式：yyyyMMdd
     * @return 手机号列表
     */
    public Set<String> getAllDelBlackUserByDate(String dateStr) {
        String key = getKey(dateStr);
        Map<String, String> delBlackUserMap = redisClient.hgetAll(key);
        return delBlackUserMap.keySet();
    }

    private String getKey(String dateStr) {
        return DEL_BLACK_USER_HEAD + ":" + dateStr;
    }
}
